package com.ujoku.controller.oa;

import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.platform.web.rest.exception.InvalidRequestException;
import com.labillusion.core.util.StringUtils;
import com.ujoku.context.ActionContext;
import com.ujoku.domain.Member;
import com.ujoku.service.MemberService;
import com.ujoku.view.domain.oa.OAMemberView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by Greg on 2014/10/20.
 */
@RestController
public class OAMemberController extends RESTController {

    private static Logger logger = LoggerFactory.getLogger(OAMemberController.class);

    @Autowired
    public MemberService service;

    @Autowired
    protected ActionContext actionContext;

    @RequestMapping(value = "/uj-oa-api/member/{id}", method = RequestMethod.GET)
    @ResponseBody
    public OAMemberView getById(@PathVariable String id,HttpServletRequest request){
        if(!StringUtils.hasText(id)){
            throw new InvalidRequestException("id不能为空");
        }

        String ascription = (String) request.getSession().getAttribute("ascription");

        Member member = (Member) service.selectById(id);

        if(member == null)
            return null;

        if(!ascription.equals("all") && !ascription.equals(member.getAscription()))
            throw new InvalidRequestException("无权限查看该会员的信息");

        OAMemberView view = new OAMemberView();


        BeanUtils.copyProperties(member,view);


        return view;
    }


}

